<?php
/**
 * 
 * @author tangbl
 *
 */
class RecycleAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index(){
		
		if(IS_AJAX){
			
			import('ORG.Util.Page');
				
			$table     = $this->_get("table","trim","Article");
			$model     = M($table);
			
			$orderDesc = $this->_get("orderDirection","trim","desc");
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "isdel=1 ".$map;
			$count     = $model->where($map)->count();
			
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			$upd       = L('art_recyc_page_title');
			$del       = L('art_del_page_title');
			$revocation= L('art_revocation_page_title');
			foreach($list AS $row){
				$entry = array('id[]'=>$row['id'],
					'cell'=>array(
						'id[]'     => $row['id'],
						'addtime'  => date('Y-m-d',$row['addtime']),
						'title'    => $row['title'],
						'deal'     => '
					<a class="btn blue icn-only recycone"  title="'.$upd.'" module="'.$table.'" data-id="'.$row['id'].'"><i class="icon-undo"></i></a>
				    <a class="btn red icn-only  delAlone"  title="'.$del.'" module="'.$table.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>',
					)
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			
			$array   = F("Module_cate");
			if(is_array($array)){
				foreach ($array as $k=>$v){
					$title = dejsonByLang($v[1]);
					$array[$k][1] = $title;
				}
				$modules = array_merge(C("RECYCLE_CATEGORY"),$array);
			}else{
				$modules = C("RECYCLE_CATEGORY");
			}
			
			$select = '';
			foreach($modules as $k=>$v){
				$select .= '<option value="'.$k.'">'.L($v[1]).'</option>';
			}
			$this->assign("select",$select);
			
			$lang = C('LANG_ARRAY');
			$this->assign("lang_filter",$lang);
			
			$this->display();
		}
	}
	
	/**
	 * 物理删除
	 */
	public function del(){
		$id    = $this->_get('id',"intval",0);
		$table = $this->_get('table');
		if(!$id || !$table) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model   = M("$table");
		$where   = "id in ($id)";
		$res  = $model->where($where)->delete();
		
		if($res) {	
			wlog(1);
			$this->success(L('cfg_form_tipmsg_delsucc'));
		} else {
			$this->error(L('cfg_form_tipmsg_delfail'));
		}
	}
	
	/**
	 * 恢复数据
	 */
	public function recyc(){
		$id    = $this->_get('id',"intval",0);
		$table = $this->_get('table');
		if(!$id || !$table) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model   = M("$table");
		$where   = "id in ($id)";
		$map["isdel"] = 2 ;
		$res  = $model->where($where)->save($map);
		
		if($res) {
			wlog(1);
			$this->success(L('art_form_tipmsg_recsucc'));
		} else {
			$this->error(L('art_form_tipmsg_recfail'));
		}
	}
	

}